/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 * Created on 5.5.2011, 9:49:59
 */

package src.clovece;

import java.awt.Point;
import java.net.*;
import java.io.*;
import src.clovece.game.GameInfo;
import src.clovece.game.Multiplayer;

/**
 *
 * @author Thomas
 */
public class LoginDialog extends javax.swing.JDialog {
    javax.swing.JDialog parent = null;
    GameFrame father = null;
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();
   

    /** Creates new form LoginDialog */
    public LoginDialog(javax.swing.JDialog parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        Point p = parent.getLocation();
        this.setLocation(p.x +200, p.y +100);
        
    }

    public LoginDialog(GameFrame father, boolean modal) {
        super(father, modal);
        this.father = father;
        Point p = father.getLocation();
        this.setLocation(p.x +200, p.y +100);

    }

    public void showYourself() {
        initComponents();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        NicknameLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        LoginButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        NicknameTextField = new javax.swing.JTextField();
        PasswordField = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Login");

        NicknameLabel.setText("Nickname:");

        PasswordLabel.setText("Password:");

        LoginButton.setText("Login");
        LoginButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LoginButtonMouseClicked(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });

        NicknameTextField.setColumns(10);

        PasswordField.setColumns(10);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(LoginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                        .addComponent(CancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(NicknameLabel)
                            .addComponent(PasswordLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                            .addComponent(NicknameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                            .addComponent(jLabel1))))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NicknameLabel)
                    .addComponent(NicknameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PasswordLabel)
                    .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelButton)
                    .addComponent(LoginButton))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents



    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        if (this.parent != null)
            this.parent.dispose();
        
        this.dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

    private void LoginButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LoginButtonMouseClicked
        String usr = new String();
        String pswd = new String();
        usr = NicknameTextField.getText();
        char [] pwd = PasswordField.getPassword();

         Multiplayer mp = new Multiplayer(this);

        for (int i =0; i < pwd.length; i++)
        {
            pswd += pwd[i];
        }
         

        if (mp.login(usr, pswd) == true)
        {
            if (parent != null)
            {
                this.dispose();
                this.parent.setVisible(true);
            }
            if (father != null)
            {
                mp.writeJustLogin();
                String message = mp.readMessage();
                if (message.equals("NoGame"))
                {
                    ErrorDialog ed = new ErrorDialog(father, true, "There are no created games");
                    ed.setVisible(true);
                    System.out.println("There are no created games");
                    this.dispose();
                }
                else{
                    System.out.println("There is a created game");

                    this.gameInfo.setGameBoardSize(Integer.parseInt(mp.readMessage()));
                    this.gameInfo.setNumberOfPlayers(Integer.parseInt(mp.readMessage()));

                    String nm = null;
                    for(int i=0; i<this.gameInfo.getNumberOfPlayers();i++) {
                        nm = mp.readMessage();
                        if(usr.equals(nm))
                            this.gameInfo.setYourPosition(i);
                        this.gameInfo.setPlayerNickname(nm, i);
                    }
                    int[] array = new int[6];
                    for(int i=0; i<6;i++) {
                        array[i] = Integer.parseInt(mp.readMessage());
                    }
                    this.gameInfo.setGeneratedValues(array);
                    this.gameInfo.setLocalGame(false);
                    this.gameInfo.setJoinedGame(true);
                    System.out.println("Your position is: " + this.gameInfo.getYourPosition());
                    this.dispose();
                    this.father.fillGameFrame();
                }
            }
        }
    }//GEN-LAST:event_LoginButtonMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton LoginButton;
    private javax.swing.JLabel NicknameLabel;
    private javax.swing.JTextField NicknameTextField;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

}
